package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.legal.domain.vo.XjclBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Xjcl;
import com.ruoyi.legal.service.IXjclService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 虚假材料公司登记Controller
 *
 * @author xwj
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/legal/xjcl")
@Api(tags = "虚假材料公司登记")
public class XjclController extends BaseController {
    @Autowired
    private IXjclService xjclService;

    /**
     * 查询虚假材料公司登记列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Xjcl xjcl) {
        startPage();
        List<Xjcl> list = xjclService.selectXjclList(xjcl);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Xjcl xjcl) {
        startOrderBy();
        List<Xjcl> list = xjclService.selectXjclList(xjcl);
        return success(list);
    }

    /**
     * 导出虚假材料公司登记列表
     */
    @Log(title = "虚假材料公司登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Xjcl xjcl) {
        List<Xjcl> list = xjclService.selectXjclList(xjcl);
        ExcelUtil<Xjcl> util = new ExcelUtil<Xjcl>(Xjcl.class);
        util.exportExcel(response, list, "虚假材料公司登记数据");
    }

    /**
     * 获取虚假材料公司登记详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(xjclService.selectXjclById(id));
    }

    @PostMapping("/batchSaveUpdate")
    @ApiOperation("批量保存")
    public ResponseModel<XjclBatchVo> batchSaveUpdate(@RequestBody XjclBatchVo xjclBatchVo) {
        SysUser user = getLoginUser().getUser();
        xjclService.batchSaveUpdate(xjclBatchVo, user);
        return ResponseModel.of(xjclBatchVo);
    }

    /**
     * 新增虚假材料公司登记
     */
    @Log(title = "虚假材料公司登记", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Xjcl xjcl) {
        SysUser user = getLoginUser().getUser();
        if (xjcl.getId() == null) {
            xjcl.setStatus("0"); // 0未提交，1已提交
            xjcl.setCreateId(user.getUserId());
            xjcl.setCreateTime(DateUtils.getNowDate());
            xjcl.setCreateBy(user.getNickName());

            if(StringUtils.isEmpty(xjcl.getSlsj())) {
                String nowStr = DateUtil.formatDateTime(DateUtil.date());
                xjcl.setSlsj(nowStr);
            }

            xjclService.insertXjcl(xjcl);
        } else {
            xjcl.setUpdateId(user.getUserId());
            xjcl.setUpdateTime(DateUtils.getNowDate());
            xjcl.setUpdateBy(user.getNickName());
            xjclService.updateXjcl(xjcl);
        }

        return success(xjcl);
    }

    /**
     * 修改虚假材料公司登记
     */
    @Log(title = "虚假材料公司登记", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Xjcl xjcl) {
        SysUser user = getLoginUser().getUser();
        xjcl.setUpdateId(user.getUserId());
        xjcl.setUpdateTime(DateUtils.getNowDate());
        xjcl.setUpdateBy(user.getNickName());
        return success(xjclService.updateXjcl(xjcl));
    }

    /**
     * 删除虚假材料公司登记
     */
    @Log(title = "虚假材料公司登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(xjclService.deleteXjclByIds(ids));
    }

    @Log(title = "虚假材料公司登记", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Xjcl xjcl) {
        int num = xjclService.delete(xjcl);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Xjcl>> queryPage(@RequestBody Xjcl xjcl) {
        Pages<Xjcl> page = xjclService.queryPage(xjcl);
        return ResponseModel.of(page);
    }
}
